package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.WholeHouseCaseDAO;
import com.ecoolex.ying.space.entity.WholeHouseCase;
import com.ecoolex.ying.space.entity.WholeHouseCase.Column;
import com.ecoolex.ying.space.entity.WholeHouseCaseExample;
import com.ecoolex.ying.space.entity.WholeHouseCaseExample.Criteria;
import com.ecoolex.ying.space.pojo.AddWholeHouseCaseItemsDTO;
import java.sql.Date;
import java.time.Instant;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class WholeHouseCaseManager {
	
    @Autowired
    private WholeHouseCaseDAO dao;
    
    @Autowired
    private WholeHouseCaseItemsManager wholeHouseCaseItemsManager;
    
    @Autowired
    private ShareImageManager shareImageManager;
    
    private void verify(WholeHouseCase record,List<AddWholeHouseCaseItemsDTO> itemsDTOList) {
		BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getName()), ResultCode.VERIFY.build(10), "案例名称为空");
		BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getCoverImage()), ResultCode.VERIFY.build(11), "案例封面为空");
		for (AddWholeHouseCaseItemsDTO item : itemsDTOList) {
			BizAssert.isTrue(Check.notNullOrTrimEmpty(item.getName()), ResultCode.VERIFY.build(12), "栏目名称为空");
			BizAssert.isTrue(Check.notNull(item.getImages()), ResultCode.VERIFY.build(13), "栏目图片为空");
		}
	}
    
    @Transactional
    public WholeHouseCase add(WholeHouseCase record,List<AddWholeHouseCaseItemsDTO> itemsDTOList, String createUser) {
    	verify(record,itemsDTOList);
    	record.setOnline(true);
    	record.setDeleted(WholeHouseCase.NOT_DELETED);
		record.setCreateUser(createUser);
		record.setCreateTime(Date.from(Instant.now()));
		record.setUpdateUser(createUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.insert(record);
		
		// 添加分享图片
        shareImageManager.addShareImage(record.getCoverImage());
		
		wholeHouseCaseItemsManager.updateWholeHouseCaseItems(record, itemsDTOList, createUser);
    	return record;
    }
    
    @Transactional
    public WholeHouseCase update(WholeHouseCase record,List<AddWholeHouseCaseItemsDTO> itemsDTOList, String updateUser) {
    	verify(record,itemsDTOList);
    	record.setUpdateUser(updateUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(record, Column.excludes(Column.online,Column.deleted,Column.createUser,Column.createTime));
    	
    	// 添加分享图片
        shareImageManager.addShareImage(record.getCoverImage());
    	
    	//更新栏目明细
    	wholeHouseCaseItemsManager.updateWholeHouseCaseItems(record, itemsDTOList, updateUser);
    	return record;
    }
    
    @Transactional
    public void delete(WholeHouseCase record, String updateUser) {
    	record.setDeleted(WholeHouseCase.IS_DELETED);
    	record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.deleted,Column.updateTime,Column.updateUser);
		wholeHouseCaseItemsManager.delete(record, updateUser);
    }
    
    public void online(WholeHouseCase record, String updateUser) {
    	record.setOnline(true);
    	record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.online,Column.updateTime,Column.updateUser);
    }
    
    public void offline(WholeHouseCase record, String updateUser) {
    	record.setOnline(false);
    	record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.online,Column.updateTime,Column.updateUser);
    }
    
    public WholeHouseCase getById(Integer caseId) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(caseId, WholeHouseCase.NOT_DELETED);
    }
    
    public WholeHouseCase getByIdForH5(Integer caseId) {
    	WholeHouseCaseExample example = new WholeHouseCaseExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andCaseIdEqualTo(caseId).andOnlineEqualTo(true).andDeletedEqualTo(WholeHouseCase.NOT_DELETED);
    	return dao.selectOneByExample(example);
    }
    
    public Pagination<WholeHouseCase> query(Boolean online,String name, Sorting[] sorts, Page page){
    	WholeHouseCaseExample example = new WholeHouseCaseExample();
    	Criteria criteria = example.createCriteria();
    	if(Check.notNullOrTrimEmpty(name)) {
    		criteria.andNameLike('%' + name
			+ '%');
    	}
    	if(Check.notNull(online)) {
    		criteria.andOnlineEqualTo(online);
    	}
    	criteria.andDeletedEqualTo(WholeHouseCase.NOT_DELETED);
		example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());

		return Pagination.create(page, dao.countByExample(example), () -> dao.selectByExample(example));
    }
    
    public WholeHouseCase getWholeHouseCaseByOutsourcingPackagedId(Integer outsourcingPackagedId) {
    	return dao.getWholeHouseCaseByOutsourcingPackagedId(outsourcingPackagedId);
    }
    
    /**
     * 根据商品Id查询关联的案例列表
     * @param outsourcingGoodsId
     * @param size
     * @return
     */
    public List<WholeHouseCase> listWholeHouseCaseByOutsourcingGoodsId(Integer outsourcingGoodsId,Integer size){
    	return dao.listWholeHouseCaseByOutsourcingGoodsId(outsourcingGoodsId, size);
    }
    
    public Pagination<WholeHouseCase> queryByOutsourcingGoodsId(Integer outsourcingGoodsId,Page page){
    	return Pagination.create(page, 
    		dao.countByOutsourcingGoodsId(outsourcingGoodsId),
    		() -> dao.queryByOutsourcingGoodsId(outsourcingGoodsId, page));
    }
}